package com.hyopt.modules.app.service.impl;



import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hyopt.common.exception.MyException;
import com.hyopt.common.exception.ServiceException;
import com.hyopt.common.log.LogEnum;
import com.hyopt.common.log.LogUtils;
import com.hyopt.modules.app.dao.UserDao;
import com.hyopt.modules.app.entity.UserEntity;
import com.hyopt.modules.app.form.LoginForm;
import com.hyopt.modules.app.form.UserInfoForm;
import com.hyopt.modules.app.service.UserService;
import com.sun.org.apache.xpath.internal.operations.Bool;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.User;
import org.apache.ibatis.jdbc.Null;
import org.springframework.stereotype.Service;

import java.sql.Struct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

/**
 * TODO UserService 实现类
 *
 * @author crazypenguin
 * @version 1.0.0
 * @createdate 2019/1/9
 */
@Service("userService")
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {

    //Logger login_log = (Logger) LogUtils.Logger(LogEnum.LOGIN_LOGS);

    @Override
    public UserEntity checking(LoginForm form) throws MyException {
        UserEntity entity = this.getOne(new QueryWrapper<UserEntity>()
                .eq("username", form.getUsername()));
        //login_log.info(entity.getRealname());
        if(entity == null){
            throw new MyException("账号不存在");
        }
        if(!entity.getPassword().equals(form.getPassword())){
            throw new MyException("密码错误");
        }
        //保存token
        this.updateInfo(form.getToken(),entity);
        entity.setHeadimg(entity.getHeadimg().trim());
        return entity;
    }

    @Override
    public Boolean updateInfo(UserInfoForm form) {
        UserEntity entity = this.getById(form.getId());
        if(!StrUtil.isBlank(form.getHeadimg())){
            entity.setHeadimg(form.getHeadimg());
        }
        if(!StrUtil.isBlank(form.getOldPassword())){
            if(form.getOldPassword().equals(entity.getPassword())){
                entity.setPassword(form.getPassword());
            }else{
                throw new MyException("旧密码不正确");
            }
        }
        if(!StrUtil.isBlank(form.getMobile())){
            entity.setMobile(form.getMobile());
        }
        return this.updateById(entity);
    }

    public void updateInfo(String token,UserEntity entity) throws MyException{
        if(StrUtil.isBlank(token)){
            throw new MyException("token为空");
        }
        entity.setToken(token);
        this.updateById(entity);
    }

    @Override
    public void logout(Integer id) {
        this.baseMapper.tokenIsNull(id);
    }

  @Override
  public Integer selectGrouopnumber(Integer uid) {
    return this.baseMapper.selectGrouopnumber(uid);
  }

  @Override
  public List<String> selectDefence(Integer uid) {
    return this.baseMapper.selectDefence(uid);
  }




}
